﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using C.Unite;
using C.Entities;
using ChanExt;
using MySoft.Data;
using System.Text;
using LitJson;

namespace ArtLand.OA.Project.PublicModel
{
    public partial class SelectContat : CommonPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid(GetWhere());
            }
        }


        #region BindGrid
        private void BindGrid(WhereClip wc)
        {
            Grid1.RecordCount = TbsDbSession.Default.From<Proj_Contat>().Where(wc).Count();

            List<Proj_Contat> items = TbsDbSession.Default.From<Proj_Contat>().Where(wc).OrderBy(Proj_Contat._.ID.Desc).Page(Grid1.PageSize, (Grid1.PageIndex + 1)).ToList();

            // 3.绑定到Grid
            Grid1.DataSource = items;
            Grid1.DataBind();
        }



        protected void Grid1_PageIndexChange(object sender, ChanExt.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid(GetWhere());
        }

        public WhereClip GetWhere()
        {
            WhereClip wc = WhereClip.All;

            if (!string.IsNullOrEmpty(Request["pid"]))
            {
                Proj_Main pm = TbsDbSession.Default.From<Proj_Main>().Where(Proj_Main._.ID == int.Parse(Request["pid"])).ToFirst();
                wc = wc && Proj_Contat._.PRJNUB == pm.PRJNUB;
            }
            if (!String.IsNullOrEmpty(ttbxMyBox2.Text))
            {
                wc = wc && (Proj_Contat._.CONNUB.Contain(ttbxMyBox2.Text) || Proj_Contat._.CONDES.Contain(ttbxMyBox2.Text));
            }
            return wc;
        }
        // 点击 TwinTriggerBox 的搜索按钮
        protected void ttbxMyBox2_Trigger2Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(ttbxMyBox2.Text))
            {
                // 执行搜索动作
                BindGrid(GetWhere());
                ttbxMyBox2.ShowTrigger1 = true;
            }
            else
            {
                Alert.ShowInTop("请输入你要搜索的关键词！");
            }
        }

        // 点击 TwinTriggerBox 的取消按钮
        protected void ttbxMyBox2_Trigger1Click(object sender, EventArgs e)
        {
            // 执行清空动作

            ttbxMyBox2.Text = "";
            ttbxMyBox2.ShowTrigger1 = false;
            BindGrid(GetWhere());
        }
        #endregion

        protected void Grid1_RowDataBound(object sender, GridRowEventArgs e)
        {
            //string color = e.Values[7].ToString();
            //string bold = e.Values[8].ToString();
            //if (bold == "True")
            //    bold = "bold";
            //else
            //    bold = "";
            //string title = "<span style=\"color:{0};font-weight:{1}\">" + e.Values[0] + "</span>";
            //title = string.Format(title, color, bold);
            //e.Values[0] = title;
            //e.Values[7] = "<a href=\"javascript:;\" onclick=\"TabView('/AfterSale/ASale_RePairChange.aspx?id=" + e.Values[7] + "', 'tab_wxadd', '修改维修记录', '/icon/comment_edit.png');\"><img src=\"/icon/comment_edit.png\" /></a>";
        }


        protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e)
        {
            string id = Grid1.DataKeys[e.RowIndex][0].ToString();
            string name = Grid1.DataKeys[e.RowIndex][1].ToString();
            Proj_Contat con = TbsDbSession.Default.From<Proj_Contat>().Where(Proj_Contat._.ID == id).ToFirst();
            object ob = TbsDbSession.Default.From<Proj_Podata>().Where(Proj_Podata._.CONNUB == name).Select(Proj_Podata._.TAMOUT.Sum()).ToScalar();
            string s = "";
            if (!string.IsNullOrEmpty(Request["s"]))
            {
                PageContext.RegisterStartupScript(ActiveWindow.GetWriteBackValueReference(con.CONNUB, name) + ActiveWindow.GetHidePostBackReference());
            }
            else
            {
                if (ob.ToString() != "")
                    s = string.Format("<b>合同的总金额为：{1}</b><br><br><span style='color:red;font-weight:bold;'>该合同的现采购合计总额为：{0}</span>", ob, con.AMOUNT.Value);
                else
                    s = string.Format("<b>合同的总金额为：{0}</b>", con.AMOUNT.Value);
                PageContext.RegisterStartupScript(ActiveWindow.GetWriteBackValueReference(con.AMOUNT.Value.ToString() + "-" + ob.ToString(), name, s) + ActiveWindow.GetHidePostBackReference());
            }
        }

    }
}